package com.chengwei.ch.busdemo.activity;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.chengwei.ch.busdemo.R;
import com.chengwei.ch.busdemo.base.BaseActivity;
import com.chengwei.ch.busdemo.stomp.Message;

import static com.chengwei.ch.busdemo.APP.app;
import static com.chengwei.ch.busdemo.APP.stomp;


@SuppressLint("Registered")
public class SignInActivityCopy extends BaseActivity {
    private Button btnSignIn, btnDriverLogin, btnRegister;
    private TextView tvDesSignIn;

    private String queueName = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_in);
        initView();
        initClickListener();
    }

    private void initView() {
        //开启stomp连接
        app.startService();
        // mScreensaver.exitApp();
        btnSignIn = findViewById(R.id.btnCheckSignIn);
        btnDriverLogin = findViewById(R.id.btnDriverLoginSignIn);
        btnRegister = findViewById(R.id.btnDriverLoginRegister);
        tvDesSignIn = findViewById(R.id.tvDesSignIn);
        initData();
    }

    private void initData() {
        String tcsiId = "eb68463f30d5444e8092300af8959cfd";
        queueName = "/" + tcsiId + ".waitPay.queue";
        Log.d("BaseActivity:", "queueName:" + queueName);
        new Handler().postDelayed(() -> {
            //订阅队列
            startQueue(queueName);
        }, 1000);
    }

    private void initClickListener() {
        btnSignIn.setOnClickListener(this);
        btnDriverLogin.setOnClickListener(this);
        btnRegister.setOnClickListener(this);
    }

    /**
     * 订阅队列
     */
    private void startQueue(String queueName) {
        if (stomp != null) {
            stomp.queue(queueName, (Message msg) -> {
                // 得到消息负载
                String message = msg.getPayload();
                Log.d("BaseActivity", "数据接收:" + message);
                tvDesSignIn.setText(message);
            });
         /*   try {
                stomp.queue(queueName, (Message msg) -> {
                    // 得到消息负载
                    String message = msg.getPayload();
                    Log.d("BaseActivity", "数据接收:" + message);
                    tvDesSignIn.setText(message);
                });
            } catch (Exception e) {
                ToastUtils.showShort("该队列已经被订阅过");
                Log.d("BaseActivity", "The destination [" + e + "] has already been subscribed!");
            }*/
        }
    }

  /*  @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull android.os.Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                Bundle data = msg.getData();
                String message = data.getString("message");
                tvDesSignIn.setText(message);
            }
        }
    };*/


    /**
     * 退订队列
     */
    private void unQueue(String queueName) {
        stomp.unqueue(queueName);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnCheckSignIn: {
                break;
            }
            case R.id.btnDriverLoginSignIn: {
                //订阅队列
                startQueue(queueName);
                break;
            }
            case R.id.btnDriverLoginRegister: {
                //解除绑定
                unQueue(queueName);
                break;
            }
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //mScreensaver.resetTime();
        app.exitApp();
        //退订队列
        unQueue(queueName);
    }
}
